package com.pin.zheng.xingmulibrary.base;


import com.pin.zheng.xingmulibrary.http.net.RxScheduler;
import com.pin.zheng.xingmulibrary.view.IBaseView;

import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Observer;

//import io.reactivex.rxjava3.core.Observable;
//import io.reactivex.rxjava3.core.Observer;

public abstract class BasePresenter<V> {
    protected V mView;


    /**
     * 绑定view，一般在初始化中调用该方法
     *
     * @param view view
     */
    public void attachView(V view) {
        this.mView = view;
    }

    /**
     * 解除绑定view，一般在onDestroy中调用
     */

    public void detachView() {
        this.mView = null;
    }

    /**
     * View是否绑定
     *
     * @return true ,false
     */
    public boolean isViewAttached() {
        return mView != null;
    }

    protected void asynExecution(Observable<BaseObjectBean<?>> Observable, IBaseView mView, Observer<BaseObjectBean<?>> Observer) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }

        Observable.compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())//解决内存泄漏
                .subscribe(Observer);
//                .subscribe(Observer);
    }


}
